package com.migo.controller;

import com.migo.entity.ProductEntity;
import com.migo.entity.SysUserEntity;
import com.migo.service.ImageService;
import com.migo.service.ProductService;
import com.migo.service.SysUserService;
import com.migo.utils.PageUtils;
import com.migo.utils.Query;
import com.migo.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.migo.utils.ShiroUtils.getUserId;


/**
 * 产品列表管理
 *
 * @author powervision
 * @email wes.wang@powervision.me
 * @date 2018-12-18 11:44:33
 */
@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    private ProductService productService;


    @Autowired
    private ImageService imageService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ProductEntity> productList = productService.queryList(query);
        int total = productService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(productList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 列表
     */
    @RequestMapping("/proList")
    @RequiresPermissions("product:proList")
    public R proList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        List<ProductEntity> productList = productService.queryList(params);
        return R.ok().put("proList", productList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:info")
    public R info(@PathVariable("id") Integer id) {
        ProductEntity product = productService.queryObject(id);

        return R.ok().put("product", product);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:save")
    public R save(@RequestBody ProductEntity product) {
        if (!StringUtils.isEmpty(product.getProImage())) {
            String url = imageService.uploadOne(product.getProImage());
            SysUserEntity user = sysUserService.queryObject(getUserId());
            product.setAddUser(user.getUsername());
            product.setProImage(url);
            productService.save(product);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:update")
    public R update(@RequestBody ProductEntity product) {
        if (!StringUtils.isEmpty(product.getProImage())) {
            String url = imageService.uploadOne(product.getProImage());
            product.setProImage(url);
        }
        productService.update(product);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:delete")
    public R delete(@RequestBody Integer[] ids) {
        productService.deleteBatch(ids);

        return R.ok();
    }

}
